home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5659 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Initialising structure members - help please!
  5. Date: Tue, 20 Feb 1996 11:05:53 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31298EF1.5F2C@cmt.lpr.mail.carel.fi>
  8. References: <4gb8hn$3m8@news.mistral.co.uk>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Mike Barnard wrote:
  16. > Hi all.
  17. > I'm a learner. I am trying to create a function to display a list of
  18. > menu items using a doubly linked list. My problem is in initialising
  19. > the members of the individual menu items.
  20. > Within a function called VPCMENU.C I have created a structure...
  21. > // Define a structure to hold the menu item information.
  22. > struct  menuitem
  23. >         {
  24. >         int number;                     // Item 1, item 2 etc.
  25. >         char description[70];           // Room for a text description
  26. >         struct menuitem *next;          // Pointer to next menu item
  27. >         struct menuitem *last;          // Pointer to last menu item
  28. >         };
  29. > Now I create 5 instances of variables of the type menuitem...
  30. > // Define the instances of the structure.
  31. > struct menuitem one,two,three,four,five;
  32. > Now, the fun (not) bit. I tried to initialise the items with...
  33. > one.number = 1;
  34. > one.description = "1.   Calculate a minefield";
  35. > one.next = two;
  36. > one.last = five;
  37. > The first one passes OK, but the second one creates an error. As it
  38. > stands it says "Lvalue required". Help says "The left side of an
  39. > assignment operator must be an addressable expression." I don't know
  40. > about the third and fourth ones. Yet.
  41.  
  42. Since you declared 'description' as an array of chars, use strcpy(one.description, 
  43. "whatever string"); Be sure you won't use longer strings than the space available. A 
  44. better approach would be to declare 'decription' as a 'char *' and then malloc 
  45. enough space for whatever string you're going to place into it. (Also, your syntax 
  46. would work in that case.) The last two will work if you add & in front of them.
  47.  
  48. > This is an almost exact copy of the structure examples in my book.
  49. > (The beginners guide to C by Wrox). Page 387 for those who have it.
  50. > So, can anyone help please? (I must get snippets!). This is for a PC
  51. > in text mode using Borland C++ 3.0. The completed program is to do
  52. > calculations for the e-mail game VGA Planets.
  53. > Thanks.
  54.  
  55. You're welcome,
  56.  AriL
  57. -- 
  58. All my opinions are mine and mine alone.
  59.